require "Common/define"
require "Logic/Config"
require "Common/functions"

RedDotsHeroWearHandler = {};
local this = RedDotsHeroWearHandler;

function RedDotsHeroWearHandler.init()
    --每次初始化清理旧数据
    CloseEvent(Config.EventType.Fresh_Level, this.updateLevel);
    CloseEvent(Config.EventType.Fresh_Equip, this.update);
    CloseEvent(Config.EventType.Fresh_Mercenary, this.update);
    CloseEvent(Config.EventType.Fresh_HeroLink, this.update);
    CloseEvent(Config.EventType.Fresh_Bag, this.update);

    ListenEvent(Config.EventType.Fresh_Level, this.updateLevel);
    ListenEvent(Config.EventType.Fresh_Equip, this.update);
    ListenEvent(Config.EventType.Fresh_Mercenary, this.update);
    ListenEvent(Config.EventType.Fresh_HeroLink, this.update);
    ListenEvent(Config.EventType.Fresh_Bag, this.update);
    return this;
end

--参数1：指定英雄Id，nil代表检查所有英雄
--参数2：指定部位Id，nil代表检查所有部位
function RedDotsHeroWearHandler.getFlag(...)
    local partnerId, part, includeOneLevel, mainHero = ...;
    if part ~= nil and tonumber(part) == -10000 then
        part = nil;
    end
    if includeOneLevel ~= nil and tonumber(includeOneLevel) == -10000 then
        includeOneLevel = nil;
    end
    if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.HeroLink, false) then
        mainHero = nil;
    end
    local flag = this.DoGetFlag(partnerId, part, includeOneLevel, mainHero);
    return flag;
end
function RedDotsHeroWearHandler.DoGetFlag(partnerId,part,includeOneLevel,mainHero)
    local flag = Config.RedDotsFlagState.None;
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.HeroEquip, false) then
        --local partnerId, part = ...;
        local heroList = nil;
        if partnerId ~= nil then
            heroList = {};
            local hero = logicMgr.HeroManager.getHeroDatabyPartnerId(partnerId, true);
            if hero ~= nil then
                if mainHero == nil or tonumber(mainHero) ~= 1 then
                    table.insert(heroList, hero);
                else
                    local tempHeroLink = logicMgr.HeroLinkManager.getMainList();
                    for i = 1, #tempHeroLink do
                        if tostring(partnerId) == tostring(tempHeroLink[i]) then
                            table.insert(heroList, hero);
                            break;
                        end
                    end
                end
            end
            --local level = logicMgr.HeroManager.getHeroShowLevelByPartnerId(hero.partnerId);
            --if this.isLevel and level > 2 then
            --    return nil;
            --end
        else
            heroList = {};
            local tempHeroLink = logicMgr.HeroLinkManager.getMainList();
            for i = 1, #tempHeroLink do
                local hero = logicMgr.HeroManager.getHeroDatabyPartnerId(tempHeroLink[i], true);
                if hero ~= nil then
                    table.insert(heroList, hero);
                end
            end
        end
        for i = 1, #heroList do
            local hero = heroList[i];
            if part ~= nil then
                flag = this.getFlagByHeroAndPart(hero, part,includeOneLevel);
                if flag == Config.RedDotsFlagState.Needful then
                    break ;
                end
            else
                for key, value in pairs(EnumConst.EquipPartEnum) do
                    flag = this.getFlagByHeroAndPart(hero, value,includeOneLevel);
                    if flag == Config.RedDotsFlagState.Needful then
                        break ;
                    end
                end
                if flag == Config.RedDotsFlagState.Needful then
                    break ;
                end
            end
        end
    end
    return flag
end
function RedDotsHeroWearHandler.getFlagByHeroAndPart(hero, part,includeOneLevel)
    local equip = logicMgr.EquipManager.getEquip(hero.partnerId, part);
    local level = logicMgr.HeroManager.getHeroShowLevelByPartnerId(hero.partnerId);
    local seekEquip= logicMgr.EquipManager.getAllEquipbyPart(part, hero["type"]);
    if includeOneLevel or level > 1 then
        if seekEquip ~= nil then
            if equip == nil or equip.quality < seekEquip.quality then
                return Config.RedDotsFlagState.Needful
            elseif equip.quality == seekEquip.quality then
                local equipLv = equip.curRfLevel
                local seekEquipLv1 = seekEquip.curRfLevel
                if equipLv < seekEquipLv1 then
                    return Config.RedDotsFlagState.Needful
                end
            end
        end
        --[[local equipLv = 0;
        if seekEquip1 ~= nil and (equip == nil or equip.suit == 0 or equip.suit == seekEquip1.suit) then
            if equip == nil or equip.quality < seekEquip1.quality then
                return Config.RedDotsFlagState.Needful;
            elseif equip.quality == seekEquip1.quality then
                if equip.curRfLevel ~= nil then
                    equipLv = tonumber(equip.curRfLevel)
                end
                local seekEquipLv1 = 0;
                if seekEquip1.curRfLevel ~= nil then
                    seekEquipLv1 = tonumber(seekEquip1.curRfLevel)
                end
                if equipLv < seekEquipLv1 then
                    return Config.RedDotsFlagState.Needful;
                end
            end
        end
        if seekEquip2 ~= nil and (equip == nil or equip.suit == 0 or equip.suit == seekEquip2.suit) then
            if equip == nil or equip.quality < seekEquip2.quality then
                return Config.RedDotsFlagState.Needful;
            elseif equip.quality == seekEquip2.quality then
                if equip.curRfLevel ~= nil then
                    equipLv = tonumber(equip.curRfLevel)
                end
                local seekEquipLv2 = 0;
                if seekEquip2.curRfLevel ~= nil then
                    seekEquipLv2 = tonumber(seekEquip2.curRfLevel)
                end
                if equipLv < seekEquipLv2 then
                    return Config.RedDotsFlagState.Needful;
                end
            end
        end]]
    end
    

    return Config.RedDotsFlagState.None;
end

function RedDotsHeroWearHandler.IsBattleHero(hero)
    local defaultFormation = logicMgr.FormationManager.GetFormation(EnumConst.FormationType.Duplicate);
    if defaultFormation ~= nil then
        for k, v in pairs(defaultFormation) do
            if tostring(v) == tostring(hero.partnerId) then
                return true;
            end
        end
    end
    return false;
end

function RedDotsHeroWearHandler.updateLevel()
    this.isLevel = true;
    RedDotsManager.addBroadcast(Config.RedDotsType.HeroWear);
end

function RedDotsHeroWearHandler.update()
    this.isLevel = nil;
    RedDotsManager.addBroadcast(Config.RedDotsType.HeroWear);
end